<template>
	<view class="content">
		<uv-navbar title="" :placeholder="true" @leftClick="leftClick"></uv-navbar>
		<view class="content_box">
			<view class="content_box_title">
				手机登录
			</view>
			<view class="content_box_input">
				<view class="name">手机号</view>
				<input type="text" v-model="shouji" placeholder="请输入手机号"/>
			</view>
			<view class="content_box_input">
				<view class="name">图形验证码</view>
				<input type="text" v-model="validdate" placeholder="请输入图形验证码"/>
				<image mode="widthFix" class="yzm" @click="getCode" :src="'data:image/png;base64,'+imgCode"></image>
			</view>
			<view class="content_box_input">
				<view class="name">短信验证码</view>
				<input type="text" v-model="MobileCode" placeholder="请输入短信验证码"/>
				<view class="getyzm" @tap="getWrap">{{iswrap?'获取验证码':`${wrapTime}秒重新获取`}}</view>
			</view>
			<view class="content_box_text">
				<text @click="link('/user/login/mima')">密码登录</text>
				<text @click="link('/user/login/retrieve')">忘记密码</text>
			</view>
			<view class="content_box_button" @click="denglu">
				登录
			</view>
			<view class="content_box_zc" @click="link('/user/login/register')">注册新账号</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref
	} from 'vue'
	import {
		homeApi
	} from "@/api/index"
	import {
		onLoad,
		onShow
	} from '@dcloudio/uni-app'
	import {
		isPhone,
		isEmail,
		link
	} from "@/utils/utils.js"
	
	onLoad(()=>{
		getCode()
	})
	// 获取验证
	const imgCode = ref('')
	const validkey = ref('')
	const getCode = async()=>{
		let data = {
			do:"verifycode2",
			sizemode:2,
		}
		let res = await homeApi.publicGet(data)
		imgCode.value = res.columns[0].imagedata
		validkey.value = res.columns[0].validkey2
	}
	const leftClick = () => {
		uni.navigateBack({
			delta: 1
		})
	}
	// 验证码倒计时
	const iswrap = ref(true)
	const wrapTime = ref(60)
	const validdate = ref('')
	const getWrap = () => {
		if (iswrap.value) {
			getTelCode()
		}
	}
	const shouji = ref('')
	const MobileCode = ref('')
	const getTelCode = async()=>{
		if(shouji.value==''){
			uni.showToast({
				title: '请输入手机号',
				icon:'none',
				duration: 2000
			});
			return
		}
		if(!isPhone(shouji.value)){
			uni.showToast({
				title: '请输入正确的手机号',
				icon:'none',
				duration: 2000
			});
			return
		}
		let data = {
			SendToPhone:shouji.value,
			validdate:validdate.value,
			validkey2:validkey.value
		}
		let res = await homeApi.publicPost(data,'sendmobilecode')
		if(res.columns[0].error_no=='0'){
			iswrap.value=false
			wrapTime.value=60
			uni.showToast({
				title:res.columns[0].error_msg,
				icon:'none',
				duration: 2000
			});
			const countdownId  = setInterval(()=>{
				if (wrapTime.value > 0) {
					wrapTime.value--;
				} else {
					clearInterval(countdownId);
					iswrap.value=true
				}
			},1000)
		}else{
			iswrap.value=true
			uni.showToast({
				title: res.columns[0].error_msg,
				icon:'none',
				duration: 2000
			});
		}
	}
	const userInfo = ref({})
	const denglu = async()=>{
		let data = {
			shouji:shouji.value,
			MobileCode:MobileCode.value
		}
		let res = await homeApi.publicPost(data,'usermobilelogin')
		if (res.error_no == '0') {
			for (let i = 0; i < res.columns.length; i++) {
				if (res.columns[i].name == 'userinfo') {
					userInfo.value = res.columns[i]
				}
			}
			uni.setStorageSync('token', res.token)
			if(userInfo.value.openid=='ok'){
				uni.showToast({
					title: '登录成功！',
					icon: 'success',
					duration: 2000
				});
				setTimeout(() => {
					uni.navigateBack({
						delta:2
					})
				}, 2000)
			}
			isUserCode()
		} else {
			uni.showToast({
				title: res.error_msg,
				icon: 'success',
				duration: 2000
			});
		}
	}
	const isUserCode = () => {
		uni.login({
			provider: 'weixin', //使用微信登录
			success: function(loginRes) {
				wxlogin(loginRes.code);
			}
		})
	}
	const wxlogin = async (code) => {
		let data = {
			do: 'wxcodelogin',
			code: code,
		}
		let res = await homeApi.publicGet(data)
		uni.showToast({
			title: '登录成功！',
			icon: 'success',
			duration: 2000
		});
		setTimeout(() => {
			uni.navigateBack({
				delta:2
			})
		}, 2000)
	}
</script>

<style lang="scss" scoped>
	.content {
		width: 100%;
		height: 100%;
		display: flex;
		flex-direction: column;

		&_box {
			width: 100%;
			flex: 1;
			display: flex;
			flex-direction: column;
			align-items: center;
			&_title{
				color: #e94f06;
				font-size:42rpx;
				font-weight: bold;
				margin-top: 5vh;
				padding-bottom: 7vh;
			}
			&_input{
				display: flex;
				align-items: center;
				width: 90%;
				border-bottom: 1px solid #dededf;
				height: 90rpx;
				margin-top: 30rpx;
				.name{
					font-size: 32rpx;
					width: 190rpx;
				}
				input{
					flex: 1;
					height: 90rpx;
					font-size: 30rpx;
					margin-right: 20rpx;
				}
				.getyzm{
					padding: 10rpx 20rpx;
					border: 1px solid #e94f06;
					border-radius: 30px;
					font-size: 30rpx;
					color: #e94f06;
				}
				.yzm{
					width: 200rpx;
				}
			}
			&_text{
				width: 90%;
				display: flex;
				align-items: center;
				justify-content: space-between;
				font-size: 30rpx;
				color: #e94f06;
				margin-top: 20rpx;
			}
			&_button{
				width: 90%;
				height: 100rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				color: #fff;
				background: #e94f06;
				margin-top: 60rpx;
				font-size: 36rpx;
				border-radius: 30px;
			}
			&_zc{
				width: 90%;
				height: 100rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				font-size: 36rpx;
			}
		}
	}
</style>